#include <iostream>
using namespace std;

long long pf[110000],pfNum,mn,d[110000],n,m,t,change;

long long ipow(long long dn,long long up)
{
	long long ret = 1;
	while(up--) ret *= dn;
	return ret;
}
void getChange(long long btm,long long now,long long top)
{
	long long get = m, i;
	if(now == top)
	{
		for(i=0; i < top; i++) 
			get /= d[i];
		change += long long (ipow(get,n));
	}
	else 
	{
		for(i=btm;i<pfNum;i++) d[now]=pf[i],getChange(i+1,now+1,top);
	}
}
int main()
{
	long long i,ans,mm;
	while(cin >> n >> m)
	{
		pfNum = 0; 
		mm = m;
		for(i = 2; i * i <= mm; i++)
		{
			if(mm % i == 0)
			{
				while(mm % i == 0) 
					mm /= i;
				pf[pfNum++] = i;
			}
		}

		if(mm != 1) 
			pf[pfNum++] = mm;
		ans = mn = long long (ipow(m,n));
		for(i = 0;i < pfNum; i++)
		{
			change = 0;
			getChange(0,0,i+1);
			if(i % 2) 
				ans += change;
			else 
				ans -= change;
		}
		cout << ans << endl;
	}
	return 0;
}